package com.example.dairenkun.neufoodshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.bean.Login;

import com.example.dairenkun.neufoodshop.model.UserModel;
import com.example.dairenkun.neufoodshop.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {
    private Button btn_reg;
    private EditText et_user;
    private EditText et_pass;
    private  Button btn_login;
    private CheckBox rem;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();

        initEvents();
        readSp();
        //initData();
        /*if(rem.isChecked()){
            et_user.setText(sp.getString("username",""));
            et_pass.setText(sp.getString("userpass",""));
        }*/
    }
    public void initViews(){
        sp=this.getSharedPreferences("UserInfo", MODE_PRIVATE);
        btn_reg=(Button)findViewById(R.id.btn_register);
        btn_login=(Button)findViewById(R.id.btn_login);
        et_user=(EditText)findViewById(R.id.username);
        et_pass=(EditText)findViewById(R.id.password) ;
        rem=(CheckBox)findViewById(R.id.login_check);
        }
     public void initEvents(){


       btn_login.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

               UserModel userModel=new UserModel();
               Call call=userModel.Login(et_user.getText().toString(),et_pass.getText().toString());
//3
               Callback <Login> callback=new Callback<Login>() {
                   @Override
                   public void onResponse(Call<Login> call, Response<Login> response) {
                       String userid=response.body().getUserid();
                       if("0".equals(userid))
                       {
                           //失败处理
                           Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                       } else
                       {
                           SaveUser(userid);
                           //登录成功处理
                           Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                           startActivity(intent);
                           Toast.makeText(getApplication(), "登陆成功", Toast.LENGTH_SHORT).show();
                           finish();
                       }

                   }

                   @Override
                   public void onFailure(Call<Login> call, Throwable t) {
                       Toast.makeText(LoginActivity.this, "登录失败!", Toast.LENGTH_SHORT).show();
                   }
               };
               call.enqueue(callback);
           }
       });


        //点击注册
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });


    }
    public void initData(){

    }
    public void SaveUser(String userid){
        final SharedPreferences sharedPreferences=getSharedPreferences("UserInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
        //int uid=Integer.parseInt(userid);
        String uid=userid;
        editor.putString("user_id", uid);
        editor.putString("username", et_user.getText().toString());
        editor.putString("userpass", et_pass.getText().toString());
        //存开关状态
        editor.putBoolean("remember", rem.isChecked());
        editor.commit();
    }
    private void readSp() {
        // TODO Auto-generated method stub
        //获取用户名
        String strname=sp.getString("username", null);
        //获取密码
        String strpass=sp.getString("userpass", null);
        //获取开关状态
        Boolean remember=sp.getBoolean("remember",false);
        if(remember)
        {
            rem.setChecked(true);
            et_user.setText(strname);
            et_pass.setText(strpass);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==RESULT_OK){
            String username=data.getStringExtra("username");
            et_user.setText(username);
        }
    }
}

